<?php
/**
 * Created by PhpStorm.
 * User: MACD
 * Date: 14-7-24
 * Time: 下午4:38
 */

class Info_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    public function create_info_sell($arr)
    {
        $this->db->insert('INFO_SELL', $arr);
        $query = $this->db->query('SELECT LAST_INSERT_ID()');
        $result = $query->row_array();
        return $result['LAST_INSERT_ID()'];
    }

    public function create_info_buy($arr)
    {
        $this->db->insert('INFO_BUY', $arr);
        $query = $this->db->query('SELECT LAST_INSERT_ID()');
        $result = $query->row_array();
        return $result['LAST_INSERT_ID()'];
    }

    /*
     * $is_sell: 是车主还是乘客
     * $route: begin-end
     * $pageindex: 分页的页索引 默认每页10条记录
     * return: 匹配结果
     */
    public function match_fun($is_sell, $begin, $end, $time, $pageindex, $user_id)
    {
        if(($begin<$end)&&(!$is_sell))//路径是正向且是乘客找车主
        {
            $this->db->from('INFO_SELL');
            $where = "BEGIN_POINT < END_POINT AND BEGIN_POINT <= $begin AND END_POINT >= $end AND UNIX_TIMESTAMP(U_TIME) = UNIX_TIMESTAMP('$time') AND CURRENT_NUM < MAX_NUM AND USER_ID != $user_id";
            $this->db->where($where);
            $this->db->order_by('CREATE_TIME', 'desc ');
            $this->db->limit(10, $pageindex);
            $result = $this->db->get();
            return $result->result_array();
        }
        if(($begin>$end)&&(!$is_sell))//路径反向且是乘客找车主
        {
            $this->db->from('INFO_SELL');
            $where = "BEGIN_POINT > END_POINT AND BEGIN_POINT >= $begin AND END_POINT <= $end AND UNIX_TIMESTAMP(U_TIME) = UNIX_TIMESTAMP('$time') AND CURRENT_NUM < MAX_NUM AND USER_ID != $user_id";
            $this->db->where($where);
            $this->db->order_by('CREATE_TIME', 'desc ');
            $this->db->limit(10, $pageindex);
            $result = $this->db->get();
            return $result->result_array();
        }
        if(($begin<$end)&&$is_sell)//路径正向且车主找乘客
        {
            $this->db->from('INFO_BUY');
            $where = "BEGIN_POINT < END_POINT AND BEGIN_POINT >= $begin AND END_POINT <= $end AND UNIX_TIMESTAMP(U_TIME) = UNIX_TIMESTAMP('$time') AND IS_ORDERED = 0 AND USER_ID != $user_id";
            $this->db->where($where);
            $this->db->order_by('CREATE_TIME', 'desc ');
            $this->db->limit(10, $pageindex);
            $result = $this->db->get();
            return $result->result_array();
        }
        if(($begin>$end)&&$is_sell)//路径反向且车主找乘客
        {
            $this->db->from('INFO_BUY');
            $where = "BEGIN_POINT > END_POINT AND BEGIN_POINT <= $begin AND END_POINT >= $end AND UNIX_TIMESTAMP(U_TIME) = UNIX_TIMESTAMP('$time') AND IS_ORDERED = 0 AND USER_ID != $user_id";
            $this->db->where($where);
            $this->db->order_by('CREATE_TIME', 'desc ');
            $this->db->limit(10, $pageindex);
            $result = $this->db->get();
            return $result->result_array();
        }
        return false;
    }

    public function create_orders($arr)
    {
        $this->db->where('INFO_ID', $arr['INFO_BUY_ID']);
        $this->db->update('INFO_BUY', array('IS_ORDERED'=>1));
        $id = $arr['INFO_SELL_ID'];
        $str = "UPDATE info_sell SET CURRENT_NUM=CURRENT_NUM+1 WHERE INFO_ID=$id";
        $this->db->query($str);
        return $this->db->insert('ORDERS', $arr);
    }

    /*
     * 根据用户id，作为车主在乘客表中查找
     */
    public function get_order_sell($id)
    {
        $str = "SELECT users.USER_ID,users.USER_NAME,
        info_buy.BEGIN_POINT,info_buy.END_POINT,info_buy.PHONE,
        info_buy.PRICE,info_buy.U_TIME,orders.ORDER_ID
        FROM orders,info_sell,info_buy,users
        WHERE info_sell.USER_ID=$id
        AND orders.INFO_SELL_ID=info_sell.INFO_ID
        AND info_buy.INFO_ID=orders.INFO_BUY_ID
        AND orders.CONFIRM=0
        AND orders.FINISH=0
        AND users.USER_ID=info_buy.USER_ID";
        $query = $this->db->query($str);
        return $query->result_array();
    }

    /*
     * 根据用户id，作为乘客在车主表中查找
     */
    public function get_order_buy($id)
    {
        $str = "SELECT users.USER_ID,users.USER_NAME,
        info_sell.BEGIN_POINT,info_sell.END_POINT,
        info_sell.CURRENT_NUM,info_sell.MAX_NUM,
        info_sell.PHONE,info_sell.PRICE,info_sell.U_TIME,orders.ORDER_ID
        FROM orders,info_buy,info_sell,users
        WHERE info_buy.USER_ID=$id
        AND orders.INFO_BUY_ID=info_buy.INFO_ID
        AND info_sell.INFO_ID=orders.INFO_SELL_ID
        AND orders.CONFIRM=0
        AND orders.FINISH=0
        AND users.USER_ID=info_sell.USER_ID";
        $query = $this->db->query($str);
        return $query->result_array();
    }

    public function get_order_finished($id)
    {
        $str = "SELECT info_buy.USER_ID AS USER_ID_B,
        info_sell.USER_ID AS USER_ID_S,info_buy.BEGIN_POINT,
        info_buy.END_POINT,info_buy.U_TIME,info_buy.PRICE,info_buy.PHONE,orders.ORDER_ID
        FROM orders INNER JOIN info_buy ON orders.INFO_BUY_ID=info_buy.INFO_ID
        INNER JOIN info_sell on orders.INFO_SELL_ID=info_sell.INFO_ID
        WHERE (orders.FINISH=1 AND info_buy.USER_ID=$id
        AND orders.INFO_BUY_ID=info_buy.INFO_ID
        AND orders.FINISH=1) OR (info_sell.USER_ID=$id
        AND orders.INFO_SELL_ID=info_sell.INFO_ID AND orders.FINISH=1)";
        $query = $this->db->query($str);
        return $query->result_array();
    }

    public function update_order($id)
    {
        $this->db->where('ORDER_ID', $id);
        return $this->db->update('ORDERS', array('CONFIRM'=>1,'FINISH'=>1));
    }

    public function create_comment($arr)
    {
        return $this->db->insert('comments', $arr);
    }

    public function get_comment($order_id)
    {
        $str = "SELECT t1.u1 AS FROM_ID,t2.u2
        AS TO_ID,t1.c1 AS CONTENT
        FROM (SELECT users.USER_NAME
        AS u1,comments.CONTENT AS c1,users.USER_ID AS i1
        FROM comments,users WHERE comments.ORDER_ID=$order_id
        AND comments.FROM_USER=users.USER_ID)
        AS t1,(SELECT users.USER_NAME
        AS u2,comments.CONTENT AS c2,users.USER_ID AS i2
        FROM comments,users WHERE comments.ORDER_ID=$order_id
        AND comments.TO_USER=users.USER_ID) AS t2 WHERE t1.i1!=t2.i2";

        $query = $this->db->query($str);
        return $query->result_array();
    }
}
